package com.nextel.project.cliente.bean;

import pe.com.nextel.rms.DataInputStreamX;
import pe.com.nextel.rms.DataOutputStreamX;
import pe.com.nextel.rms.PersistenteX;
import java.io.IOException;
import org.kxml2.io.KXmlParser;
import org.kxml2.io.KXmlSerializer;
import pe.com.nextel.util.ArrayList;
import pe.com.nextel.util.Comparable;
import pe.com.nextel.util.Iterator;
import pe.com.nextel.util.List;
import pe.com.nextel.util.StringUtils;

/**
 *
 * @author eespinoza
 */
public class ClientePorCodigoBean extends PersistenteX implements Comparable {

    private int idRecordCliente;
    private short posRecordCliente;
    private String campoBuscar;

    public void persistir(DataOutputStreamX dataOutput, List list) throws IOException {
        dataOutput.writeInt(list.size());
        
//        System.out.println("cant==" + list.size( ));
        
        for (Iterator it = list.iterator(); it.hasNext();) {
            ClientePorCodigoBean clienteCodigo = (ClientePorCodigoBean) it.next();
            dataOutput.writeInt(clienteCodigo.getIdRecordCliente());
            dataOutput.writeShort(clienteCodigo.getPosRecordCliente());
            dataOutput.writeString(StringUtils.stringNullToBlank(clienteCodigo.getCampoBuscar()));
        }
    }

    public List recuperar(DataInputStreamX dataInput) throws IOException {
        List lista = new ArrayList();
        ClientePorCodigoBean clienteCodigo = null;
        int cant = dataInput.readInt();
//        System.out.println("cant==" + cant);
        for (int i = 0; i < cant; i++) {
            clienteCodigo = new ClientePorCodigoBean();
            clienteCodigo.setIdRecordCliente(dataInput.readInt());
            clienteCodigo.setPosRecordCliente(dataInput.readShort());
            clienteCodigo.setCodigoCliente(dataInput.readString());
            lista.add(clienteCodigo);
        }
        //System.out.println("clienteCodigo= " + clienteCodigo);
        return lista;
    }

    public int getIdRecordCliente() {
        return idRecordCliente;
    }

    public void setIdRecordCliente(int idRecordCliente) {
        this.idRecordCliente = idRecordCliente;
    }

    public short getPosRecordCliente() {
        return posRecordCliente;
    }

    public void setPosRecordCliente(short posRecordCliente) {
        this.posRecordCliente = posRecordCliente;
    }

    public String getCampoBuscar() {
        return campoBuscar;
    }

    public void setCodigoCliente(String codigoCliente) {
        this.campoBuscar = codigoCliente;
    }

    public void writeXMLObject(KXmlSerializer writer) throws IOException {
    }

    public void readXMLObject(KXmlParser parser) throws IOException {
        setIdRecordCliente(Integer.parseInt(parser.getAttributeValue(null, "i")));
        setPosRecordCliente(Short.parseShort(parser.getAttributeValue(null, "p")));
        setCodigoCliente(parser.getAttributeValue(null, "r"));
    }

    //retorna negativo <0 -> this es menor al arg0
    //retorna 0 -> this = arg0
    //retorna positivo >0 -> this es mayor al arg0
    public int compareTo(Object o) {
        try {

            ClientePorCodigoBean bean = (ClientePorCodigoBean) o;

            int x = this.getCampoBuscar().length();
            int y = bean.getCampoBuscar().length();

            if (x == y) {
                return this.getCampoBuscar().compareTo(bean.getCampoBuscar());
            } else if (x > y) {
                return 1;
            } else {
                return -1;
            }

        } catch (Exception e) {
            return -1;
        }
    }

    public boolean equals(Object o) {
        try {
            ClientePorCodigoBean bean = (ClientePorCodigoBean) o;
            if (this.getCampoBuscar().equals(bean.getCampoBuscar())) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    public int hashCode() {
        if (getCampoBuscar() != null) {
            return getCampoBuscar().hashCode();
        } else {
            return 0;
        }
    }

    public void clear() {

    }
}
